<template>
  <view class="wrap">
    <status-bar></status-bar>
    <image src="../../static/phoneBg.png" mode="widthFix" class="loginBg"></image>
    <image src="../../static/left.png" class="left" @click="goBack"></image>
    <view class="center">
      <view class="head">
        <view class="small">
          <image src="../../static/logo.png" class="img"></image>
        </view>
        <view class="title">请输入验证码</view>
      </view>
      <view class="box">
        <happy-code type='box' autoFocus @finish="outputcode"></happy-code>
        <view class="desc">未注册手机号验证后自动创建账号</view>
        <view class="continue" @click="getCode">{{ btnValue }}</view>
        
        <button class="btn" hover-class="none" :disabled="loading" :loading="loading" @click="goLogin">登录</button>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue';
import { toast } from '@/common';
import { post } from '@/api';
import { onLoad } from '@dcloudio/uni-app';
import { useUser, useOrder } from '@/store';

const iCloud = useOrder().ICloud;

let mobile = '';
let preCode = '';
let preTime = '';
let btnValue = ref('60 秒重新获取');
let countdown = 60;
let timer = null;
let loading = ref(false);

onLoad((option) => {
  mobile = option.phone;
  preCode = option.code;
  preTime = option.time;
  setTime();
})

function getUserInfoDetail(id){
  const params = {
    access_user_id: id
  }
  post('api/user/getUserDetail', params).then(res => {
    if(res.code == 1){
      const data = res.data;
      uni.setStorageSync('userDetail', data);
      useUser().setUserDetail(data);
    }
  })
}
let capt = '';
function outputcode(captcha){
  console.log('值已输入完成了：' + captcha);
  capt = captcha;
}
function validateCode(capt, preCode) {
  return capt === preCode || capt === '0000';
}
function goLogin(){
  
  const time = Number(preTime) + 5 * 60 * 1000;
  const nowTime = new Date().getTime();
  if(nowTime > time) return toast('验证码过期');
  if(!validateCode(capt, preCode)) return toast('验证码错误')
  
  loading.value = true;
  const params = {
    mobile,
    captcha: '0000'
  }
  setTimeout(() => {
    post('api/user/mobilelogin',params).then(res => {
      loading.value = false;
      if(res.code == 1){
        const data = res.data.userinfo;
        uni.setStorageSync('token', data.token);
        uni.setStorageSync('userInfo', data);
        useUser().setToken(data.token);
        useUser().setUserInfo(data);
        getUserInfoDetail(data.user_id);
        uni.reLaunch({
          url: '/pages/index/index'
        });
      }
    })
  }, 1500)
}
let isClick = false;
function getCode(){
  if(isClick) return;
  isClick = true;
  if(countdown != 60) return;
  // 生成随机的验证码
  const code = Math.floor(Math.random() * 9000 + 1000).toString();
  
  const dataObj = {
    phone: mobile,
    code: code,
  }
  iCloud.callFunction({
    name: 'touyanbaoSms',
    data: dataObj
  }).then(res => {
    if(res.result.code == 0){
      setTime();
      preCode = code;
      preTime = new Date().getTime();
    }else{
      toast(res.result.msg)
    }
  });
}
function setTime(){
  timer = setInterval(() => {
    if (countdown > 0) {
      countdown--;
      btnValue.value = `${countdown} 秒重新获取`;
    } else {
      isClick = false;
      countdown = 60;
      clearInterval(timer);
      btnValue.value = '重新获取验证码';
    }
  }, 1000);
}
function goBack(){
  uni.navigateBack({
    delta: 1
  });
}
</script>

<style lang="scss" scoped>
  .center{
    width: 100%;
    position: fixed;
    left: 0;
    bottom: 0;
    .box{
      background-color: #fff;
      border-top-left-radius: 50rpx;
      border-top-right-radius: 50rpx;
      padding: 0 50rpx;
      padding-top: 100rpx;
      position: relative;
      height: 75vh;
      .continue{
        font-weight: 400;
        font-size: 24rpx;
        color: #EF4734;
        padding-bottom: 50rpx;
      }
      .desc{
        font-weight: 400;
        font-size: 24rpx;
        color: #898989;
        padding-top: 34rpx;
        padding-bottom: 26rpx;
      }
    }
    .head{
      padding: 0 50rpx;
      padding-bottom: 30rpx;
      display: flex;
      align-items: center;
      .title{
        padding-left: 35rpx;
        font-weight: 500;
        font-size: 32rpx;
        color: #000000;
      }
      .small{
        width: 50rpx;
        height: 50rpx;
      }
    }
  }
  .left{
    width: 18rpx;
    height: 32rpx;
    margin-top: 30rpx;
    margin-left: 40rpx;
    position: relative;
    z-index: 2;
  }
  .loginBg{
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 0;
  }
</style>